<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <ul>
      <li v-for="cate in cates" :key="cate.id">
        <h5>{{cate.name}}</h5>
        <img :src="cate.icon" alt="">
      </li>
    </ul>
    <ul>
      <li v-for="item in items" :key="item.id">
        <img :src="item.pic" width="300" style="display:block" alt="">
        <h5>商品名称：{{item.name}}</h5>
        <p>商品价格：{{item.minPrice}}</p>

      </li>
    </ul>
  </div>
  <script src="./vue.js"></script>
  <script src="./axios.js"></script>
  <script>
    const request = axios.create({
      baseURL: 'https://api.it120.cc/conner'
    })

    // 添加一个请求拦截
    request.interceptors.request.use(function (config) {
      // 可以在发送之前做点什么
      console.log('请求拦截器触发了');
      return config;
    }, function (error) {
      // 请求出错的拦截 如果不return catch执行不了
      return Promise.reject(error);
    });

    // 添加响应拦截
    request.interceptors.response.use(function (response) {
      console.log('响应拦截触发了');
      /* 
      只要 http状态码是2xx 会进入这个响应成功拦截器中
  
      */
      return response;
    }, function (error) {
      /* 
      响应失败 
      http状态不是 2xx其他状态码时 进来
      */
      return Promise.reject(error);
    });


    const vm = new Vue({
      el: '#app',
      data: {
        cates: [],
        items: []
      },
      created() {
        this.fetchCates();
        this.fetchItems();
      },
      methods: {
        fetchCates() {
          request.get('/shop/goods/category/all', {
            params: {
              a: 10,
              b: 20
            }
          }).then(res => {
            if (res.data.code === 0) {
              this.cates = res.data.data
            }
          })
        },
        fetchItems() {
          request({
            method: 'post',
            url: '/shop/goods/list',
            data: {
              a: 10,
              b: 1000
            }
          }).then(res => {
            if (res.data.code === 0) {
              this.items = res.data.data;
            }
          })
        }
      }
    })
  </script>
</body>

</html>